<?php

namespace app\api\service;

use app\common\model\Order;
use app\common\model\Secretkey;
use app\common\model\User;
use app\common\model\UserAmount;
use app\common\model\UserAmountRewardDetailed;
use app\Response;
use think\facade\Cache;

class UserService
{
    /** 用户中心
     * @param $user_id int
     * @param $t_id int
     * @return json
    */
    public static function userCenterService($user_id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[21,22,23,24])){
            return Response::wrapError(0,"账户非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $info["userInfo"] = $userInfo;
        $userAmount = UserAmount::userAmountInfo(["uid"=>$user_id]);
        $ua_where[] = ["uid","=",$user_id];
        $ua_where[] = ["state","=",2];
        $ua_where[] = ["type","in","1,2,3"];
        $userAmountCount = UserAmountRewardDetailed::orderRewardSum($ua_where,"amount_money");
        $info["balance"] = $userAmount["balance"];
        $info["commission"] = 0;//暂时无表
        $info["invite_reward_money"] = $userAmountCount;
        $n_where[] = ["state",">",0];
        $n_where[] = ["addtime",">",strtotime(date("Y-m-d",time()))];
        if($userInfo['type'] == 21){
            $n_where[] = ["ps_id","=",$user_id];
            $next_order = Order::dataCount($n_where);
            $next_order_sum = Order::orderSum($n_where,"low_price");
            $today_order_user_num = Order::distinctCount($n_where);
            $info["today_order_num"] = $next_order;//暂时无表
            $info["today_order_sum"] = $next_order_sum;//暂时无表
            $info["today_order_user_num"] = $today_order_user_num;//暂时无表
        }elseif($userInfo['type'] == 22){
            $n_where[] = ["sup_uid","=",$user_id];
            $next_order = Order::dataCount($n_where);
            $next_order_sum = Order::orderSum($n_where,"low_price");
            $today_order_user_num = Order::distinctCount($n_where);
            $info["today_order_num"] = $next_order;//暂时无表
            $info["today_order_sum"] = $next_order_sum;//暂时无表
            $info["today_order_user_num"] = $today_order_user_num;//暂时无表
        }
        return Response::wrapData($info);
    }


    /*** 修改用户信息
     * @param $user_id int
     * @param $data array
     * @return json
     */
    public static function editUserInfoService($user_id,$data){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(empty($userInfo)){
            return Response::wrapError('非法请求',-200);
        }
        if(!in_array($userInfo['type'],[22,23,24])){
            return Response::wrapError(0,"账户非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError('此账户已被禁用,如有疑问请联系客服人员',-200);
        }
        $up = [];
        if(!empty($data["nickname"])){
            $up["nickname"] = $data["nickname"];
        }
        if(!empty($data["photo"])){
            $up["photo"] = $data["photo"];
        }
        if(!empty($data["province"])){
            $up["province"] = $data["province"];
        }
        if(!empty($data["city"])){
            $up["city"] = $data["city"];
        }
        if(!empty($up)){
            $res = User::userEdit(["uid"=>$user_id],$up);
        }else{
            $res = true;
        }
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError("微信号绑定修改失败,请稍后重试");
        }
    }


    //生成邀请码
    public static function extensionCreat(){
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $rand = $code[rand(0,25)] .strtoupper(dechex(date('m'))) .date('d').substr(time(),-5) .substr(microtime(),2,5) .sprintf('%02d',rand(0,99));
        for( $a = md5( $rand, true ), $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $d = '', $f = 0; $f < 8; $g = ord( $a[ $f ] ), $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ], $f++ );
        $superorUser = \app\common\model\User::userInfo("extension = binary '$d'"); //邀请码采用二进制 区分大小写
        if($superorUser){
            return self::extensionCreat();
        }
        return $d;
    }

}